﻿///<reference path="../../ExtJs/vswd-ext_2.2.js" />
var pro_id;
var pageSize = 15;  // Grid每页显示个数
var cellWidth1 = 100; //单元格宽度
var cellWidth2 = 100;
Ext.onReady(function() {
    Ext.QuickTips.init();
    // 本地加载图片代码
    Ext.BLANK_IMAGE_URL = strPath + 'extjs/resources/images/default/s.gif';

    pageSize = parseInt(((parent.Ext.get("ContentFrame").getHeight() - 101) - 1) / 21); //根据页面高度显示记录个数
    cellWidth1 = parseInt((parent.Ext.get("ContentFrame").getWidth() - 24) / 5);
    cellWidth2 = parseInt(parent.Ext.get("ContentFrame").getWidth() - 24 - (cellWidth1 * 4));

    var httpProxy;
    httpProxyProductOrderList = new Ext.data.HttpProxy({
        url: strPath + 'GGFW/DocumentsManage/ProductOrderCommand.fwf?Method=SearchProductOrderList'
    });
    // 监测任务数据对应字段的读取方式
    var readerProxyProductOrderList = new Ext.data.JsonReader({
        root: 'Records',
        id: 'HWTransferItemID',
        totalProperty: 'totalCount',
        fields: [
				{ name: 'pro_id' },
			    { name: "code" },
			    { name: "pro_name" },
			    { name: "reviewer" },
			    { name: "designer" },
			    { name: "date" }
           ]
    });

    // 定义生产单数据源
    storeProductOrderList = new Ext.data.Store({
        proxy: httpProxyProductOrderList,
        reader: readerProxyProductOrderList,
        remoteSort: true
    });

    // 生产单grid的选择列的事件
    var smProductOrderListModel = new Ext.grid.CheckboxSelectionModel({
        singleSelect: false,
        listeners: {
            rowselect: function(sm, row, rec) {
                pro_id = rec.get("pro_id");
            }
        }
    });

    // 设置显示列的Title
    var ChooseView = new Ext.grid.GridView({ forceFit: true, sortAscText: '升序', sortDescText: '降序' });
    ChooseView.columnsText = '列显示/隐藏';

    // 设置页脚的显示样式。
    var ChooseBar = new Ext.PagingToolbar({
        pageSize: pageSize,
        store: storeProductOrderList,
        beforePageText: '第',
        afterPageText: '页 共{0}页',
        firstText: '第一页',
        prevText: '前一页',
        nextText: '后一页',
        lastText: '最后一页',
        refreshText: '刷新',
        displayInfo: true,
        displayMsg: '显示第 {0} - {1} 条数据, 共 {2} 条数据',
        emptyMsg: "没有数据可以显示"
    });


    // 污染源监测任务Grid显示内容
    var ColumnModelProductOrderList = new Ext.grid.ColumnModel([
        smProductOrderListModel,
        { id: 'pro_id', header: "产品ID", align: 'center', sortable: false, dataIndex: 'pro_id', hidden: true },
		{ header: "编号", align: 'center', sortable: true, dataIndex: 'code', width: cellWidth1, renderer: renderValue },
		{ header: "款名", align: 'center', sortable: true, dataIndex: 'pro_name', width: cellWidth1, renderer: renderValue },
		{ header: "审核", align: 'center', sortable: true, hidden: true, dataIndex: 'reviewer', width: cellWidth1, renderer: renderValue },
		{ header: "设计", align: 'center', sortable: true, dataIndex: 'designer', width: cellWidth1, renderer: renderValue },
		{ header: "日期", align: 'center', sortable: true, dataIndex: 'date', renderer: renderDateValue, width: cellWidth1, hidden: false },
		{ header: "操作", align: 'center', sortable: false, dataIndex: 'pro_id', width: cellWidth2, renderer: getitemlink }

     ]);
    /**
    *描述 : 渲染列，鼠标移该单元格上面，显示完整的值
    *参数 : value：单元格的值；
    */
    function renderValue(value, metadate, record, colIndex, rowIndex) {
        return '<div style="text-align:left;"><span title="' + value + '">' + value + '</span></div>';
    }

    //定义行记录
    var Record = new Ext.data.Record.create([
        { name: 'pro_id', type: 'int' },
        { name: 'client_name', type: 'string' },
        { name: 'code', type: 'string' },
        { name: 'pro_name', type: 'string' },
        { name: 'reviewer', type: 'string' },
        { name: 'designer', type: 'string' },
        { name: 'date', type: 'string' }
    ]);

    // 删除按钮
    var buttonDelProduct = {
        id: 'btnDelProduct',
        text: '<font color="#000000">删除</font>',
        handler: function() {
            DelProduct();  // 删除调用的函数
        },
        iconCls: 'add',
        icon: strPath + 'images/ico_25.gif'

    };

    var labelSearchCode = new Ext.form.Label({
        id: "lbSearchCode",
        html: "<span>编号：</span>"
    });

    var textfieldSearchCode = new Ext.form.TextField({
        id: "tfSearchCode",
        width: 128
    });

    var labelSearchProName = new Ext.form.Label({
        id: "lbSearchProName",
        html: "<span>&nbsp;&nbsp;款名：</span>"
    });

    var textfieldSearchProName = new Ext.form.TextField({
        id: "tfSearchProName",
        width: 128
    });

    // 工具条
    var toolbarProductOrderList = new Ext.Toolbar({
        height: 25,
        items: [buttonDelProduct, ' ', '-', ' ', labelSearchCode, textfieldSearchCode, labelSearchProName, textfieldSearchProName]
    });

    gridProductOrderList = new Ext.grid.EditorGridPanel({
        id: 'pagedef-ProductOrderList',
        store: storeProductOrderList,
        renderTo: "divProductOrderList",
        stripeRows: true,
        cm: ColumnModelProductOrderList,
        sm: smProductOrderListModel,
        width: parent.Ext.get("ContentFrame").getWidth(),
        height: parent.Ext.get("ContentFrame").getHeight() - 25,
        loadMask: { msg: '正在加载数据，请稍侯... ...' },
        frame: false,
        border: false,
        animCollapse: false,
        trackMouseOver: true,
        bbar: ChooseBar,
        tbar: toolbarProductOrderList
    });

    //数据源开始读取数据
    doSearchProductOrderList(true);
})

// 加载数据(刷新)
function doSearchProductOrderList(isFirstPage) {
    if (storeProductOrderList == null) return;
    // 定义查询参数
    //var TaskName = Ext.get("txtSearchTaskName").dom.value;  // 批次任务名称
    //var SamplingTime = Ext.get("txtSearchStartTime").dom.value;  // 采样时间
    // 传递查询参数并刷新
    if (isFirstPage) storeProductOrderList.load({
        params: {
            start: 0, // 起始页
            limit: pageSize,  // 每页显示个数
            code: Ext.get("tfSearchCode").dom.value,
            pro_name: Ext.get("tfSearchProName").dom.value
        }
    });
    else storeProductOrderList.load({
        params: {
            start: (gridProductOrderList.getBottomToolbar().getPageData().activePage - 1) * pageSize,  // 起始页
            limit: pageSize,  // 每页显示个数
            code: Ext.get("tfSearchCode").dom.value,
            pro_name: Ext.get("tfSearchProName").dom.value
        }
    });
}

// 删除方法
function DelProduct() {
    var pro_ids = getSelectID(gridProductOrderList, 'pro_id');
    if (pro_ids == '' || pro_ids == null) {
        Ext.MessageBox.alert("提示", "请选择要删除的数据！"); return;
    }
    Ext.MessageBox.confirm("询问", '确认要删除选中的数据吗？', function(btn) {
        if (btn == "yes") {
            var strUrl = strPath + 'GGFW/DocumentsManage/ProductOrderCommand.fwf?Method=DelProductByProID';
            Ext.Ajax.request({
                url: strUrl,
                method: 'POST',
                params: {
                    pro_ids: pro_ids
                },
                failure: function(response, options) {
                    ReturnValue = Ext.MessageBox.alert("警告", "网络异常或登录过期，请重新登录！");
                },
                success: function(response, options) {
                    var json = response.responseText;
                    var obj = parseJsonResult(json);
                    if (!obj.Success) {
                        showMessage("错误！", "删除数据出现错误！请查看详细原因。", obj.Message);
                    }
                    else {
                        // 删除成功后刷新页面
                        doSearchProductOrderList(false);
                    }
                }
            });
        }
    });
}

//页面跳转
function getitemlink(value, metadate, record, colIndex, rowIndex) {
    return "<a class='a_link' href=\"ProductOrderEdit.aspx?pro_id=" + record.get("pro_id") + "\">编辑</a>"; // | <a class='a_link' href='#' onclick='PrintProduct(" + record.get("pro_id") + ");'>打印生产单</a>
}